//
//  AddCourseVC.m
//  优学荟管理系统
//
//  Created by 星道 on 17/8/24.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "AddCourseVC.h"
#import "AddCourseCell.h"
#import "ZYCurriculumMajorCategoryView.h"
#import "ZYBigCourseModel.h"
#import "ZYBigCourseTowModel.h"
#import "ZYBigCourseThreeModel.h"
#import "ZYDatePickerView.h"
#import "HNPickView.h"
#import "HNRegion.h"
#import "ZYLMultiselectView.h"
#define Screen_Width ([UIScreen mainScreen].bounds.size.width)
#define Screen_Height ([UIScreen mainScreen].bounds.size.height)
@interface AddCourseVC ()<UITableViewDelegate,UITableViewDataSource,AddCourseCellDelegate,ZYDatePickerViewDelegate>
/** 菊花 */
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activity;
/** tableView */
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) NSMutableArray *dataSourceTow;
@property (nonatomic,strong) ZYBigCourseModel *model;
@property (nonatomic,strong) ZYBigCourseTowModel *modelTow;
@property (nonatomic,strong) AddCourseCell *cell;

@property (nonatomic,strong) NSMutableArray *dataSourceThree;
@property (nonatomic,strong) ZYBigCourseThreeModel *modelThree;
@property (nonatomic,strong) ZYDatePickerView *datePickerView;
@property (nonatomic, strong) NSDate *stareDate;
@property (nonatomic, copy) NSString *creatStartTime;
@property (nonatomic,assign) NSInteger tag;
@property (nonatomic,copy)NSString *bigID;//大课ID
@property (nonatomic,copy)NSString *smallID;//小课ID
@property (nonatomic,copy)NSString *ID;//模板ID
/** 地址选择器 */
@property (nonatomic, strong) HNPickView *PickView;
/** 地区文件 */
@property (nonatomic, strong) HNRegion *Region;
/** 临时输入框 */
@property (nonatomic, strong) UITextField *tempTextField;

@property (nonatomic, assign) BOOL startBool;
@property (nonatomic, assign) BOOL endBool;
@property (nonatomic, assign) BOOL timeBool;
@property (assign, nonatomic) BOOL isBool;
@property (strong, nonatomic) NSMutableArray *arr;
@property (strong, nonatomic) NSMutableArray *resultArr;
@property (nonatomic,copy)NSString *mmm;
@property (nonatomic,copy)NSString *mmmID;
@property (nonatomic,copy)NSString *mmmmSmatId;
@end

@implementation AddCourseVC


- (ZYDatePickerView *)datePickerView{
    if (_datePickerView == nil) {
        self.datePickerView = [[ZYDatePickerView alloc]initWithFrame:self.view.bounds type:UIDatePickerModeDateAndTime];
        self.datePickerView.delegate = self;
        self.datePickerView.optionalMinDate = [NSDate date];
    }
    return _datePickerView;
}

- (NSMutableArray *)dataSourceThree {
    
    if (_dataSourceThree == nil) {
        self.dataSourceThree = [NSMutableArray array];
    }
    return _dataSourceThree;
    
    
}



- (NSMutableArray *)dataSourceTow {
    
    if (_dataSourceTow == nil) {
        self.dataSourceTow = [NSMutableArray array];
    }
    
    return _dataSourceTow;
}


- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isBool = YES;
    self.arr = [NSMutableArray array];
    self.resultArr = [NSMutableArray array];
    
    // 增加课使用
    if (self.mode == AddCourseAdd) {
        
        
        self.title = @"添加课程";
        /** 网络数据请求成功之后使用 */
        [self.activity stopAnimating];
        /** 设置内容可显示 */
        self.tableView.hidden = NO;
    } else {
        
        // 修改课程
        self.title = @"编辑课程";
        [self ConfigUploadUI];
    }
    
    
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    self.stareDate = currentDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm"];
    self.creatStartTime = [dateFormatter stringFromDate:currentDate];
    
    // 地区
    [self ConfigPickView];
    
    // tableView配置
    [self.tableView registerNib:[UINib nibWithNibName:[AddCourseCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.rowHeight = 1100;
}

#pragma mark --- 大类目接口
- (void)setNetWork{
    // http://192.168.0.113:8080/StarShine/ys/lesson.do?action=getBigCategory
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getBigCategory"];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [MBProgressHUD showHUD];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [self.dataSource removeAllObjects];
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSArray *arr = [Obj valueForKey:@"t"];
            
            for (NSDictionary *dict  in arr) {
                ZYBigCourseModel *model = [ZYBigCourseModel new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
            }
            
            
            [self.tableView reloadData];
            
            [self curriculumOne];
        }
        
        
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
    
    
}
#pragma mark --- 小类目接口
- (void)setNetWorkTow:(UITextField *)textField{
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getSmallCategoryByBigId&bigId=%@",self.bigID];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [MBProgressHUD showHUD];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [self.dataSourceTow removeAllObjects];
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSArray *arr = [Obj valueForKey:@"t"];
            
            for (NSDictionary *dict  in arr) {
                ZYBigCourseTowModel *model = [ZYBigCourseTowModel new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSourceTow addObject:model];
                
            }
            
            
            [self.tableView reloadData];
            
            [self curriculumTow:textField];
        }
        
        
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
    
    
}



#pragma mark --- 膜拜接口
- (void)setNetWorkThree{
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getTemplates"];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [MBProgressHUD showHUD];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [self.dataSourceThree removeAllObjects];
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            
            NSArray *arr = [t  valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in arr) {
                ZYBigCourseThreeModel *model = [ZYBigCourseThreeModel new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSourceThree addObject:model];
            }
            [self.tableView reloadData];
            
            if (self.dataSourceThree.count != 0) {
                [self curriculumThree];
            }else{
                self.ID = @"0";
                [self createAlertViewWithTitle:nil message:@"没有数据" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            }
            
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
    
    
}



#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.Delegate = self;
    self.cell = cell;
    //    cell.deleteCourse.hidden = self.mode == AddCourseAdd ? YES:NO;
    // 删除课程
    cell.ClickDeleteCourse = ^() {
        [self MgrDeleteCourse];
    };
    return cell;
}

#pragma mark - 界面代理

/** 单击输入框触发事件,tag值详见.h文件注释 */
- (void)cell:(AddCourseCell *)cell ClickWithTextField:(UITextField *)textField tag:(NSInteger)tag{
    
    
    [self.view endEditing:YES];
    
    if (1101 == tag) {//课程课程大类目
        
        [self setNetWork];
        
        
    }else if (1102 == tag || 1111 == tag){//课程小类目
        
        
        if (self.bigID) {
            [self setNetWorkTow:textField];
            
        }else{
            
            NSLog(@"请先选择大类");
            [self createAlertViewWithTitle:nil message:@"请先选择大类" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        }
        
        
        
    }else if (1103 == tag){
        self.tempTextField = textField;
        [self.PickView Show:self.Region RegionType:(HNPickViewProvice)];
    }else if (1104 == tag){
        
        if (cell.province.text.length == 0) {
            [self createAlertViewWithTitle:nil message:@"请先选择省份" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        }else{
            [self.Region GetCityWithProvinceid:[self.cell.provinceNumber integerValue]];
            [self.PickView Show:self.Region RegionType:(HNPickViewCity)];
        }
        
        
    }else if (1105 == tag){
        
        if (cell.city.text.length == 0) {
            [self createAlertViewWithTitle:nil message:@"请先选择城市" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        }else{
            [self.Region GetAreaWithCityid:[self.cell.cityNumber integerValue]];
            [self.PickView Show:self.Region RegionType:(HNPickViewArea)];
        }
        
        
        
    }else if (1106 == tag){
        [self setNetWorkThree];
    }else if (2101 == tag){//课程开始时间
        self.tag = tag;
        self.datePickerView.title = @"课程开始时间";
        [self.datePickerView show];
        
    }else if (2102 == tag){//课程结束时间
        self.tag = tag;
        self.datePickerView.title = @"课程结束时间";
        [self.datePickerView show];
    }else if (2103 == tag){//上课时间
        self.tag  = tag;
        self.datePickerView.title = @"上课时间";
        [self.datePickerView show];
    }
    
    
    
    
}

/** 可以走到这里来，说明数据都填写完了，直接拿cell里的.h控件数据就行了 */
- (void)cell:(AddCourseCell *)cell ClickSureWithButton:(UIButton *)Button
{
    
    // 增加课程
    if (self.mode == AddCourseAdd) {
        double sum = [cell.price_Text.text doubleValue];//报名费用
        NSInteger coins = [cell.coins_Text.text integerValue];//星币抵扣
        NSInteger sumMoney = 0;
        NSInteger coninsStar = 0;
        
        
        
        if (sum *0.05  >= 200) {//抵扣金额超过200的情况
            sumMoney = 200;//最大抵扣星币
            
            if (coins  >= 200) {//星币抵扣数大于最大抵扣星币数 就等于最大抵扣星币数200
                coninsStar = 200;
            }else{
                coninsStar = coins;
            }
            
            
            
            
        }else{//抵扣金额小于课程
            
            if (coins > sum *0.05) {//星币抵扣数大于最大可抵扣的星币数时，给个提示
                //给个提示
                [self createAlertViewWithTitle:nil message:@"星币抵扣数不能大于最大可抵扣的星币数" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
                
            }else{
                coninsStar = coins;
                sumMoney = sum *0.05;
                
                
            }
            
        }
        
        NSMutableArray *smallCategory = [NSMutableArray array];
        self.smallID ? [smallCategory addObject:self.smallID]:nil;
        self.cell.smallTwoID ? [smallCategory addObject:self.cell.smallTwoID]:nil;
        NSString *categoryID = [smallCategory componentsJoinedByString:@","];
        [self addcourseCategoryId:self.bigID smallCategoryId:categoryID name:cell.courseTitle_Text.text startTime:[NSString stringWithFormat:@"%@:%@",self.cell.courseBeginTime_Text.text,@"00"] endTime:[NSString stringWithFormat:@"%@:%@",self.cell.courseEndTime_Text.text,@"00"] courseCount:cell.courseCount_Text.text limitNum:cell.coursePeople_Text.text provinceid:[NSString stringWithFormat:@"%@",cell.provinceNumber] cityid:[NSString stringWithFormat:@"%@",cell.cityNumber] areaid:[NSString stringWithFormat:@"%@",cell.areaNumber] address:cell.address.text classroom:cell.courseClass_Text.text templateId:self.ID price:[NSString stringWithFormat:@"%ld",(long)sum] coins:[NSString stringWithFormat:@"%ld",(long)coninsStar] description:cell.Des_Text.text lessonTime:[NSString stringWithFormat:@"%@:%@",self.cell.courseTime_Text.text,@"00"] showPrice:cell.ShowPrice.text];
        
        
        
        
    } else {
        
        // 修改课程
        [self RefreshSureUpload];
    }
    
    
    
    
    
    
}


#pragma mark --- 大分类方法的实现
- (void)curriculumOne{
    
    [ZYCurriculumMajorCategoryView showWithTitle:@"请选择课程大类目" titles:self.dataSource zYBigCourseModel:self.model zYBigCourseTowModel:self.modelTow zYBigCourseThreeModel:self.modelThree selectIndex:^(NSInteger selectIndex) {
        NSLog(@"选择了第%ld个",selectIndex);
        
        
        
        self.bigID = [NSString stringWithFormat:@"%ld",(long)selectIndex];
        
        
        
    } selectValue:^(NSString *selectValue) {
        NSLog(@"选择的值为%@",selectValue);
        self.cell.courseBigCategory_Text.text = selectValue;
        self.cell.courseSmallCategory_Text.text = nil;
        
        
        
    } showCloseButton:NO isUp:NO isUpAll:NO];
    
    
    
}

#pragma mark --- 小分类方法的实现
- (void)curriculumTow:(UITextField *)textField{
    
    
    
    
    
    ZYLMultiselectView *zyl = [[ZYLMultiselectView alloc]initWithFrame:CGRectMake(30, 50, Screen_Width - 60, Screen_Height - 150)];
    zyl.dataSource = self.dataSourceTow;
    
    [self.view addSubview:zyl];
    
    
    zyl.selectBlock = ^(NSMutableArray *selectUserId,NSMutableArray *name) {
        self.cell.courseSmallCategory_Text.text = [name componentsJoinedByString:@","];
        self.cell.smallTwoID                                        = [selectUserId componentsJoinedByString:@","];
    };
    
    
    
    
    
    
    
    
    
    //    [ZYCurriculumMajorCategoryView showWithTitle:@"请选择课程小类目" titles:self.dataSourceTow zYBigCourseModel:self.model zYBigCourseTowModel:self.modelTow zYBigCourseThreeModel:self.modelThree selectIndex:^(NSInteger selectIndex) {
    //        NSLog(@"选择了第%ld个",selectIndex);
    //
    //        if (textField == self.cell.courseSmallCategory_Text) {
    //            self.smallID = [NSString stringWithFormat:@"%ld",(long)selectIndex];
    //        } else {
    //            self.cell.smallTwoID = [NSString stringWithFormat:@"%ld",(long)selectIndex];
    //        }
    //
    //    } selectValue:^(NSString *selectValue) {
    //        NSLog(@"选择的值为%@",selectValue);
    //        self.cell.courseSmallCategory_Text.text = selectValue;
    //
    //
    //    } showCloseButton:NO  isUp:YES isUpAll:NO];
    
    
}


#pragma mark --- 模板方法的实现
- (void)curriculumThree{
    [ZYCurriculumMajorCategoryView showWithTitle:@"请选择评价模板" titles:self.dataSourceThree zYBigCourseModel:self.model zYBigCourseTowModel:self.modelTow zYBigCourseThreeModel:self.modelThree selectIndex:^(NSInteger selectIndex) {
        NSLog(@"选择了第%ld个",selectIndex);
        self.ID = [NSString stringWithFormat:@"%ld",(long)selectIndex];
    } selectValue:^(NSString *selectValue) {
        NSLog(@"选择的值为%@",selectValue);
        self.cell.evaMould_Text.text = selectValue;
        
        
    } showCloseButton:NO  isUp:NO isUpAll:YES];
    
    
    
}

#pragma mark- <WGBDatePickerViewDelegate>
//当时间改变时触发
- (void)changeTime:(NSDate *)date{
    // NSLog(@"代理监听更改时间为---%@",[self.datePickerView stringFromDate:date]);
}

//确定时间
- (void)determine:(NSDate *)date{
    
    
    //传这个就对了
    
    NSDate *towDate = [[NSDate alloc]init];
    
    if ( 2101 == self.tag) {
        self.stareDate = date;
        self.creatStartTime = [NSString stringWithFormat:@"%@:%@",[self.datePickerView stringFromDate:date],@"00"];
        self.cell.courseBeginTime_Text.text = [self.datePickerView stringFromDate:date];
        self.startBool = YES;
    }else if (2102 == self.tag){
        towDate = date;
        if (self.cell.courseBeginTime_Text.text.length == 0) return [self  createAlertViewWithTitle:nil message:@"请先选择课程开课时间" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        
        NSComparisonResult result = [self.stareDate compare:date];
        
        if (result != NSOrderedAscending) return
            [self createAlertViewWithTitle:nil message:@"课程结束时间必须大于课程开始时间" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        self.creatStartTime = [NSString stringWithFormat:@"%@:%@",[self.datePickerView stringFromDate:date],@"00"];
        self.cell.courseEndTime_Text.text = [self.datePickerView stringFromDate:date];
        self.endBool = YES;
    }else if (2103 == self.tag){
        
        if (self.cell.courseEndTime_Text.text.length == 0)  return [self  createAlertViewWithTitle:nil message:@"请先选择课程结束时间" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        
        NSComparisonResult result = [towDate compare:date];
        
        if (result != NSOrderedAscending) return
            [self createAlertViewWithTitle:nil message:@"上课时间必须小于课程结束时间" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
        self.creatStartTime = [NSString stringWithFormat:@"%@:%@",[self.datePickerView stringFromDate:date],@"00"];
        self.cell.courseTime_Text.text = [self.datePickerView stringFromDate:date];
        self.timeBool = YES;
    }
    
    
    NSLog(@"%@ %@",[self.datePickerView stringFromDate:date],self.creatStartTime);
}





- (HNPickView *)PickView
{
    if (!_PickView)
    {
        _PickView = [[[NSBundle mainBundle] loadNibNamed:[HNPickView description] owner:nil options:nil] lastObject];
        [_PickView Config];
        __weak typeof(self)weakSelf = self;
        _PickView.PickViewBlock = ^(RegionType Type, NSInteger Index)
        {
            if (Type == HNPickViewProvice)
            {
                Provice *P = weakSelf.Region.Provices[Index];
                weakSelf.cell.province.text = P.provincename;
                weakSelf.cell.provinceNumber = P.provinceid;
                weakSelf.cell.city.text = @"";
                weakSelf.cell.cityNumber = nil;
                weakSelf.cell.area.text = @"";
                weakSelf.cell.areaNumber = nil;
            }
            
            if (Type == HNPickViewCity) {
                
                City *C = weakSelf.Region.Cities[Index];
                weakSelf.cell.city.text = C.cityname;
                weakSelf.cell.cityNumber = C.cityid;
                weakSelf.cell.area.text = @"";
                weakSelf.cell.areaNumber = nil;
            }
            
            if (Type == HNPickViewArea) {
                
                Area *A = weakSelf.Region.Areas[Index];
                weakSelf.cell.area.text = A.areaname;
                weakSelf.cell.areaNumber = A.areaid == nil ? @0:A.areaid;
            }
        };
    }
    return _PickView;
}

- (HNRegion *)Region
{
    if (!_Region)
    {
        _Region = [[HNRegion alloc] init];
    }
    return _Region;
}

- (void)ConfigPickView
{
    [self.view addSubview:self.PickView];
    self.PickView.frame = CGRectMake(0, SCREENH_HEIGHT, SCREEN_WIDTH, 250);
}



/*
 courseCategoryId     大类ID
 smallCategoryId         小类ID
 name                             开课名称
 startTime                       课程开始时间
 endTime                        课程结束时间
 courseCount                课程总次数
 limitNum                        报名人数上限
 provinceid                    省份
 cityid                               市区
 areaid                            县
 address                          街道地址
 classroom                      上课课室
 templateId
 price                                报名费用
 coins                                 最大可抵扣的星币数
 description                    课程简介
 lessonTime                      上课时间
 
 */
- (void)addcourseCategoryId:(NSString *)courseCategoryId smallCategoryId:(NSString *)smallCategoryId name:(NSString *)name  startTime:(NSString *)startTime endTime:(NSString *)endTime courseCount:(NSString *)courseCount  limitNum:(NSString *)limitNum provinceid:(NSString *)provinceid cityid:(NSString *)cityid  areaid:(NSString *)areaid  address:(NSString *)address  classroom:(NSString *)classroom  templateId:(NSString *)templateId price:(NSString *)price  coins:(NSString *)coins  description:(NSString *)description  lessonTime:(NSString *)lessonTime showPrice:(NSString *)showPrice {
    
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"1" forKey:@"sellStatus"];
    [params setValue:@"saveLesson" forKey:@"action"];
    [params setValue:@"2" forKey:@"type"];
    [params setValue:courseCategoryId forKey:@"courseCategoryId"];
    [params setValue:smallCategoryId?:@"0" forKey:@"smallCategoryIdString"];
    [params  setValue:name forKey:@"name"];
    [params setValue:startTime forKey:@"startTime"];
    [params setValue:endTime forKey:@"endTime"];
    [params setValue:courseCount forKey:@"courseCount"];
    [params setValue:limitNum.length > 0 ? limitNum:@100000 forKey:@"limitNum"];
    [params setValue:provinceid forKey:@"provinceid"];
    [params setValue:cityid forKey:@"cityid"];
    [params setValue:![areaid isKindOfClass:[NSNull class]]?areaid:@0 forKey:@"areaid"];
    [params setValue:address forKey:@"address"];
    [params setValue:classroom forKey:@"classroom"];
    [params setValue:templateId forKey:@"templateId"];
    [params setValue:price forKey:@"price"];
    [params setValue:coins forKey:@"coins"];
    [params setValue:description forKey:@"description"];
    [params setValue:lessonTime forKey:@"lessonTime"];
    [params setValue:self.TeacherID forKey:@"uid"];
    [params setValue:showPrice forKey:@"showPrice"];
    
    [HUD showHUD];
    [NetManager Post:LLBaseURL(@"lesson.do") Auth:YES Params:params Success:^(id Obj) {
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            self.RefreshUI ? self.RefreshUI():nil;
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [HUD showError:Obj[@"msg"]];
        }
        
        
    } Fail:^(NSError *Error) {
        
        [HUD showError:NetErr];
    }];
    
    
    
    
}


#pragma mark - ------     修改课程     -----
/** 如果是修改课程就走这里 */
- (void)ConfigUploadUI {
    // 获取网络数据
    [self RefreshUploadDataWithSucces:^(id obj) {
        // 界面赋值
        [self ConfigDataForUI:obj[@"t"]];
    }];
}

/** 获取网络的课程数据 */
- (void)RefreshUploadDataWithSucces:(void (^)(id obj))Succes {
    
    NSDictionary *params = @{
                             @"action"  :@"getLessonById",
                             @"id"      :self.lessionID
                             };
    NSString *str = [NSString stringWithFormat:@"lesson.do?action=getLessonById&id=%@",self.lessionID];
    NSLog(@"%@ %@",LLBaseURL(str),[AccountTool shareAccountTool].currentAccount.authCode);
    [NetManager Get:LLBaseURL(@"lesson.do") Auth:YES Params:params Success:^(id Obj) {
        
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            // 成功回调
            Succes (Obj);
        } else {
            
            [self.activity stopAnimating];
            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        
        [HUD showError:NetErr];
        [self.activity stopAnimating];
    }];
}

/** 数据获取成功之后在这里赋值 */
- (void)ConfigDataForUI:(id)obj {
    
    // 大分类
    self.cell.courseBigCategory_Text.text       = [self GetString:obj[@"courseCategoryName"]];
    self.bigID                                  = [self GetString:obj[@"courseCategoryId"]];
    // 小分类
    if (![obj[@"smallIds"] isKindOfClass:[NSNull class]]) {
        // 获取第一个数据
        NSArray *arr = obj[@"smallIds"];
        NSDictionary *dict = [arr firstObject];
        self.cell.courseSmallCategory_Text.text = dict[@"categoryName"];
        
        self.smallID                            = dict[@"id"];
        self.mmm = dict[@"categoryName"];
        self.mmmID = dict[@"id"];
        self.cell.smallTwoID = dict[@"id"];
        
        // 如果有第二数据就一并获取了
        if (arr.count > 1) {
            dict = [arr lastObject];
            // self.cell.courseTwoSmallCategory_Text.text = dict[@"categoryName"];
            self.cell.courseSmallCategory_Text.text  = [NSString stringWithFormat:@"%@,%@",self.mmm,dict[@"categoryName"]];
            //            self.cell.smallTwoID                       = dict[@"id"];
            self.smallID = [NSString stringWithFormat:@"%@,%@",self.mmmID,dict[@"id"]];
            self.cell.smallTwoID = [NSString stringWithFormat:@"%@,%@",self.mmmID,dict[@"id"]];
            // 显示界面
            // [self.cell AddNewSmallCategory:self.cell.addButton];
        }
    }
    // 开课名称
    self.cell.courseTitle_Text.text             = [self GetString:obj[@"name"]];
    // 课程开始、结束时间与上课时间
    self.cell.courseBeginTime_Text.text         = [self GetString:obj[@"startTime"]];
    self.cell.courseEndTime_Text.text           = [self GetString:obj[@"endTime"]];
    self.cell.courseTime_Text.text              = [self GetString:obj[@"lessonTime"]];
    // 上课次数与上限人数
    self.cell.courseCount_Text.text             = [self GetString:[obj[@"courseCount"] stringValue]];
    self.cell.coursePeople_Text.text            = [self GetString:[obj[@"limitNum"] stringValue]];
    // 省
    self.cell.province.text                     = [self GetString:obj[@"provincename"]];
    self.cell.provinceNumber                    = @([[self GetString:obj[@"provinceid"]] integerValue]);
    // 市
    self.cell.city.text                         = [self GetString:obj[@"cityname"]];
    self.cell.cityNumber                        = @([[self GetString:obj[@"cityid"]] integerValue]);
    // 区
    self.cell.area.text                         = [self GetString:obj[@"areaname"]];
    self.cell.areaNumber                        = @([[self GetString:obj[@"areaid"]] integerValue]);
    // 地址
    self.cell.address.text                      = [self GetString:obj[@"address"]];
    // 上课课室
    self.cell.courseClass_Text.text             = [self GetString:obj[@"classroom"]];
    // 评价模板
    self.cell.evaMould_Text.text                = [self GetString:obj[@"templatename"]];
    self.ID                                     = [self GetString:[obj[@"templateId"] stringValue]] ?:@"0";
    // 报名费用与抵扣星币
    self.cell.price_Text.text                   = [NSString stringWithFormat:@"%.2f",[obj[@"price"] doubleValue]];
    self.cell.coins_Text.text                   = [self GetString:[obj[@"coins"] stringValue]];
    self.cell.ShowPrice.text                    = [self GetString:[obj[@"showPrice"] stringValue]];
    // 描述
    self.cell.Des_Text.text                     = [self GetString:obj[@"description"]];
    self.cell._textViewAlpaceholder.hidden      = self.cell.Des_Text.text.length;
    
    [self.activity stopAnimating];
    self.tableView.hidden = NO;
    
    // 最大星币抵扣数
    double coins = [self.cell.coins_Text.text doubleValue];
    double sum = [self.cell.price_Text.text doubleValue];
    double sumMoney = 0;
    double coninsStar = 0;
    if (sum *0.05  >= 200) {//抵扣金额超过200的情况
        sumMoney = 200;//最大抵扣星币
        
        if (coins  >= 200) {//星币抵扣数大于最大抵扣星币数 就等于最大抵扣星币数200
            coninsStar = 200;
        }else{
            coninsStar = coins;
        }
        
        
    }else{//抵扣金额小于课程
        
        if (coins > sum *0.05) {//星币抵扣数大于最大可抵扣的星币数时，给个提示
            //给个提示
            //            [self createAlertViewWithTitle:nil message:@"星币抵扣数不能大于最大可抵扣的星币数" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            
        }else{
            coninsStar = coins;
            sumMoney = sum *0.05;
        }
    }
    
    self.cell.deductionCoins_Text.text = [NSString stringWithFormat:@"%ld", [[NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:sumMoney]] integerValue]];
}

/** 根据字典的value返回相应的值，防止字典为空导致崩溃 */
- (NSString *)GetString:(NSString *)Str {
    
    if (Str && ![Str isKindOfClass:[NSNull class]]) {
        
        return Str;
        
    } else {
        
        return @"";
    }
}

/** 确定修改 */
- (void)RefreshSureUpload {
    
    // 最大星币抵扣数
    double coins = [self.cell.coins_Text.text doubleValue];
    double sum = [self.cell.price_Text.text doubleValue];
    double sumMoney = 0;
    double coninsStar = 0;
    if (sum *0.05  >= 200) {//抵扣金额超过200的情况
        sumMoney = 200;//最大抵扣星币
        
        if (coins  >= 200) {//星币抵扣数大于最大抵扣星币数 就等于最大抵扣星币数200
            coninsStar = 200;
        }else{
            coninsStar = coins;
        }
        
    }else{//抵扣金额小于课程
        
        if (coins > sum *0.05) {//星币抵扣数大于最大可抵扣的星币数时，给个提示
            //给个提示
            [self createAlertViewWithTitle:nil message:@"星币抵扣数不能大于最大可抵扣的星币数" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            
        }else{
            coninsStar = coins;
            sumMoney = sum *0.05;
        }
    }
    
    NSString *startTime = self.startBool ? [NSString stringWithFormat:@"%@:%@",self.cell.courseBeginTime_Text.text,@"00"]:self.cell.courseBeginTime_Text.text;
    NSString *endTime = self.endBool ? [NSString stringWithFormat:@"%@:%@",self.cell.courseEndTime_Text.text,@"00"]:self.cell.courseEndTime_Text.text;
    NSString *time = self.timeBool ?     [NSString stringWithFormat:@"%@:%@",self.cell.courseTime_Text.text, @"00"]:self.cell.courseTime_Text.text;
    
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否确认更新课程信息?" preferredStyle:(UIAlertControllerStyleActionSheet)];
    [Alert addAction:[UIAlertAction actionWithTitle:@"修改" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        [self UploadCategoryId:self.bigID smallCategoryId:self.cell.smallTwoID name:self.cell.courseTitle_Text.text startTime:startTime endTime:endTime courseCount:self.cell.courseCount_Text.text limitNum:self.cell.coursePeople_Text.text provinceid:[NSString stringWithFormat:@"%@",self.cell.provinceNumber] cityid:[NSString stringWithFormat:@"%@",self.cell.cityNumber] areaid:[NSString stringWithFormat:@"%@",self.cell.areaNumber] address:self.cell.address.text classroom:self.cell.courseClass_Text.text templateId:self.ID price:[NSString stringWithFormat:@"%.2f",sum] coins:[NSString stringWithFormat:@"%ld",(long)coninsStar] description:self.cell.Des_Text.text lessonTime:time lessionID:self.lessionID showPrice:self.cell.ShowPrice.text];
        
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [self presentViewController:Alert animated:YES completion:nil];
}

- (void)UploadCategoryId:(NSString *)courseCategoryId smallCategoryId:(NSString *)smallCategoryId name:(NSString *)name  startTime:(NSString *)startTime endTime:(NSString *)endTime courseCount:(NSString *)courseCount  limitNum:(NSString *)limitNum provinceid:(NSString *)provinceid cityid:(NSString *)cityid  areaid:(NSString *)areaid  address:(NSString *)address  classroom:(NSString *)classroom  templateId:(NSString *)templateId price:(NSString *)price  coins:(NSString *)coins  description:(NSString *)description  lessonTime:(NSString *)lessonTime lessionID:(NSNumber *)lessionID showPrice:(NSString *)showPrice {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setValue:@"updateLesson" forKey:@"action"];
    [params setValue:@"2" forKey:@"type"];
    [params setValue:courseCategoryId forKey:@"courseCategoryId"];
    [params setValue:smallCategoryId?:@"" forKey:@"smallCategoryIdString"];
    [params  setValue:name forKey:@"name"];
    [params setValue:startTime forKey:@"startTime"];
    [params setValue:endTime forKey:@"endTime"];
    [params setValue:courseCount forKey:@"courseCount"];
    [params setValue:limitNum.length <= 0 ? @"100000":limitNum forKey:@"limitNum"];
    [params setValue:provinceid forKey:@"provinceid"];
    [params setValue:cityid forKey:@"cityid"];
    [params setValue:areaid?:@0 forKey:@"areaid"];
    [params setValue:address forKey:@"address"];
    [params setValue:classroom forKey:@"classroom"];
    [params setValue:templateId forKey:@"templateId"];
    [params setValue:price forKey:@"price"];
    [params setValue:coins forKey:@"coins"];
    [params setValue:description forKey:@"description"];
    [params setValue:lessonTime forKey:@"lessonTime"];
    [params setValue:lessionID forKey:@"id"];
    [params setValue:self.TeacherID forKey:@"uid"];
    [params setValue:showPrice forKey:@"showPrice"];
    
    [HUD showHUD];
    [NetManager Post:LLBaseURL(@"lesson.do") Auth:YES Params:params Success:^(id Obj) {
        
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            [HUD showSuccess:Obj[@"msg"]];
            [self performSelector:@selector(Pop) withObject:nil afterDelay:1.5];
        } else {
            [HUD showError:Obj[@"msg"]];
        }
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
    }];
}

/** 删除课程 */
- (void)MgrDeleteCourse {
    
    // 放警示
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨" message:@"是否删除该课程，删除后将无法恢复?" preferredStyle:(UIAlertControllerStyleActionSheet)];
    [alert addAction:[UIAlertAction actionWithTitle:@"删除" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        [self MgrDeleteCourseNetWithSuccess:^{
            [HUD showSuccess:@"删除课程成功"];
            self.RefreshUI ? self.RefreshUI():nil;
            [self performSelector:@selector(Pop) withObject:nil afterDelay:1.5];        }];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

/** 发起删除的请求 */
- (void)MgrDeleteCourseNetWithSuccess:(void (^)())Success {
    
    [HUD showHUD];
    NSDictionary *params = @{
                             @"action":@"delLesson",
                             @"id":self.lessionID
                             };
    [NetManager Get:LLBaseURL(@"lesson.do") Auth:YES Params:params Success:^(id Obj) {
        [Obj[@"code"] isEqualToNumber:@1000] ? Success() : [HUD showError:Obj[@"msg"]];
    } Fail:^(NSError *Error) {
        [HUD showError:NetErr];
    }];
    
    
}

/** 课程修改成功之后返回 */
- (void)Pop {
    if (self.backPop) {
        self.backPop();
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end

